﻿using DevComponents.AdvTree;
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MachineBuildingSystem
{
    public partial class DriverCarCfgForm : XtraForm
    {
        private List<string> m_DriverList = new List<string>();
        private List<string> m_CarList = new List<string>();
        public DriverCarCfgForm()
        {
            InitializeComponent();
        }

        private void DriverCarCfgForm_Load(object sender, EventArgs e)
        {
            if(!RefreshDriver())
                MessageBox.Show(this, "刷新司机信息异常");
            if (!RefreshCar())
                MessageBox.Show(this, "刷新车辆信息异常");
        }      

        private bool RefreshDriver()
        {
            try
            {
                DriverRootNode.Nodes.Clear();
                string drivers = ConfigurationManager.AppSettings["Drivers"];
                if (!string.IsNullOrEmpty(drivers))
                {
                    foreach (string name in drivers.Split(','))
                    {
                        if (string.IsNullOrEmpty(name))
                            continue;
                        if (!m_DriverList.Contains(name))
                            m_DriverList.Add(name);
                        Node node = new Node();
                        node.Text = name;
                        DriverRootNode.Nodes.Add(node);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("加载司机异常：" + ex.ToString());
                return false;
            }
            return true;
        }

        private bool RefreshCar()
        {
            try
            {
                CarRootNode.Nodes.Clear();
                string cars = ConfigurationManager.AppSettings["CarCodeStr"];
                if (!string.IsNullOrEmpty(cars))
                {
                    foreach (string name in cars.Split(','))
                    {
                        if (string.IsNullOrEmpty(name))
                            continue;
                        if (!m_CarList.Contains(name))
                            m_CarList.Add(name);
                        Node node = new Node();
                        node.Text = name;
                        CarRootNode.Nodes.Add(node);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("加载车牌号码异常：" + ex.ToString());
                return false;
            }
            return true;
        }

        private void btnDriverAdd_Click(object sender, EventArgs e)
        {
            ActionForm form = new ActionForm();
            form.IsAdd = true;
            form.NameList = m_DriverList;
            if (DialogResult.OK != form.ShowDialog())
                return;
            string name = form.Name;
            if (!m_DriverList.Contains(name))
                m_DriverList.Add(name);
            string str = "";
            foreach (string driver in m_DriverList)
                str += driver + ",";
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfa.AppSettings.Settings["Drivers"].Value = str.TrimEnd(',');
            cfa.Save();
            ConfigurationManager.RefreshSection("appSettings");
            RefreshDriver();
        }

        private void btnDriverModify_Click(object sender, EventArgs e)
        {
            Node selectedNode = advTreeDriver.SelectedNode;
            if (selectedNode == null)
            {
                MessageBox.Show(this, "请选择需要修改的名称");
                return;
            }
            ActionForm form = new ActionForm();
            form.IsAdd = false;
            form.Name = selectedNode.Text;
            form.NameList = m_DriverList;
            if (DialogResult.OK != form.ShowDialog())
                return;
            string name = form.NewName;
            m_DriverList.Remove(selectedNode.Text);
            if (!m_DriverList.Contains(name))
                m_DriverList.Add(name);
            string str = "";
            foreach (string driver in m_DriverList)
                str += driver + ",";
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfa.AppSettings.Settings["Drivers"].Value = str.TrimEnd(',');
            cfa.Save();
            ConfigurationManager.RefreshSection("appSettings");
            RefreshDriver();
        }

        private void btnDriverDel_Click(object sender, EventArgs e)
        {
            Node selectedNode = advTreeDriver.SelectedNode;
            if (selectedNode == null)
            {
                MessageBox.Show(this, "请选择需要删除的名称");
                return;
            }
            m_DriverList.Remove(selectedNode.Text);
            string str = "";
            foreach (string driver in m_DriverList)
                str += driver + ",";
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfa.AppSettings.Settings["Drivers"].Value = str.TrimEnd(',');
            cfa.Save();
            ConfigurationManager.RefreshSection("appSettings");
            RefreshDriver();
        }

        private void btnCarAdd_Click(object sender, EventArgs e)
        {
            ActionForm form = new ActionForm();
            form.IsAdd = true;
            form.NameList = m_CarList;
            if (DialogResult.OK != form.ShowDialog())
                return;
            string name = form.Name;
            if (!m_CarList.Contains(name))
                m_CarList.Add(name);
            string str = "";
            foreach (string car in m_CarList)
                str += car + ",";
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfa.AppSettings.Settings["CarCodeStr"].Value = str.TrimEnd(',');
            cfa.Save();
            ConfigurationManager.RefreshSection("appSettings");
            RefreshCar();
        }

        private void btnCarModify_Click(object sender, EventArgs e)
        {
            Node selectedNode = advTreeCar.SelectedNode;
            if (selectedNode == null)
            {
                MessageBox.Show(this, "请选择需要修改的名称");
                return;
            }
            ActionForm form = new ActionForm();
            form.IsAdd = false;
            form.Name = selectedNode.Text;
            form.NameList = m_CarList;
            if (DialogResult.OK != form.ShowDialog())
                return;
            string name = form.NewName;
            m_CarList.Remove(selectedNode.Text);
            if (!m_CarList.Contains(name))
                m_CarList.Add(name);
            string str = "";
            foreach (string car in m_CarList)
                str += car + ",";
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfa.AppSettings.Settings["CarCodeStr"].Value = str.TrimEnd(',');
            cfa.Save();
            ConfigurationManager.RefreshSection("appSettings");
            RefreshCar();
        }

        private void btnCarDel_Click(object sender, EventArgs e)
        {
            Node selectedNode = advTreeCar.SelectedNode;
            if (selectedNode == null)
            {
                MessageBox.Show(this, "请选择需要删除的名称");
                return;
            }
            m_CarList.Remove(selectedNode.Text);
            string str = "";
            foreach (string car in m_CarList)
                str += car + ",";
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfa.AppSettings.Settings["CarCodeStr"].Value = str.TrimEnd(',');
            cfa.Save();
            ConfigurationManager.RefreshSection("appSettings");
            RefreshCar();
        }
    }
}
